package org.example;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

import java.awt.*;
import java.io.IOException;
import java.net.URL;

/**
 * 固定写法
 */
public class WordCountDriver {
    public static void main(String[] args) {
//        System.setProperty("HADOOP_USER_NAME","fengliulin");

        URL path = WordCountDriver.class.getClassLoader().getResource("");
        try {
            Configuration configuration = new Configuration();
            configuration.set("dfs.permissions","false");
            Job job = Job.getInstance(configuration);

            // 设置jar
            job.setJarByClass(WordCountDriver.class);

            // 关联 map和reducer
            job.setMapperClass(WordCountMap.class);
            job.setReducerClass(WordCountReducer.class);

            // 设置map输出的kv类型
            job.setMapOutputKeyClass(Text.class);
            job.setOutputValueClass(IntWritable.class);

            // 设置最终输出的类型
            job.setOutputKeyClass(Text.class);
            job.setOutputValueClass(IntWritable.class);

            // 设置输入路径和输出路径
            FileInputFormat.setInputPaths(job, new Path(path + "/hello.txt"));
            FileOutputFormat.setOutputPath(job, new Path("/Users/fengliulin/Documents/Testsss/a7"));

//            FileInputFormat.setInputPaths(job, new Path(args[0]));
//            FileOutputFormat.setOutputPath(job, new Path(args[1]));

//            job.waitForCompletion(true);

            System.exit( job.waitForCompletion(true) ? 0 : 1);

        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}

